package top.bingoz.wanandroid

import android.content.Context
import android.support.multidex.MultiDex
import android.widget.Toast
import com.orhanobut.hawk.Hawk
import com.tencent.bugly.Bugly
import com.tencent.bugly.beta.Beta
import com.tencent.bugly.beta.interfaces.BetaPatchListener
import com.tencent.bugly.beta.tinker.TinkerApplicationLike
import com.tencent.bugly.beta.upgrade.UpgradeStateListener
import dagger.android.AndroidInjector
import dagger.android.support.DaggerApplication
import top.bingoz.wanandroid.common.Constants
import top.bingoz.wanandroid.common.app.TimberManager
import top.bingoz.wanandroid.di.component.DaggerAppComponent
import java.util.*


/**
 * @author su
 * @date 2018/7/27
 * @description
 */
class AppApplication : DaggerApplication() {


    companion object {
        lateinit var app: AppApplication

        fun getDrawableUri(resId: Int): String {
            return Constants.Prefix.DRAWABLE + app.resources.getResourceEntryName(resId)
        }
    }

    override fun attachBaseContext(base: Context) {
        super.attachBaseContext(base)
        // you must install multiDex whatever tinker is installed!
        MultiDex.install(base)
        val tag = TinkerApplicationLike.TAG
        // 安装tinker
        Beta.installTinker()
    }


    override fun onCreate() {
        super.onCreate()
        app = this

        // 设置是否开启热更新能力，默认为true
        Beta.enableHotfix = true
        // 设置是否自动下载补丁
        Beta.canAutoDownloadPatch = true
        // 设置是否提示用户重启
        Beta.canNotifyUserRestart = true
        // 设置是否自动合成补丁
        Beta.canAutoPatch = true

        /**
         *  全量升级状态回调
         */
        Beta.upgradeStateListener = object : UpgradeStateListener {
            override fun onUpgradeFailed(b: Boolean) {

            }

            override fun onUpgradeSuccess(b: Boolean) {

            }

            override fun onUpgradeNoVersion(b: Boolean) {
                Toast.makeText(applicationContext, "最新版本", Toast.LENGTH_SHORT).show()
            }

            override fun onUpgrading(b: Boolean) {
                Toast.makeText(applicationContext, "onUpgrading", Toast.LENGTH_SHORT).show()
            }

            override fun onDownloadCompleted(b: Boolean) {

            }
        }

        /**
         * 补丁回调接口，可以监听补丁接收、下载、合成的回调
         */
        Beta.betaPatchListener = object : BetaPatchListener {
            override fun onPatchReceived(patchFileUrl: String) {
                Toast.makeText(applicationContext, patchFileUrl, Toast.LENGTH_SHORT).show()
            }

            override fun onDownloadReceived(savedLength: Long, totalLength: Long) {
                Toast.makeText(applicationContext, String.format(Locale.getDefault(),
                        "%s %d%%",
                        Beta.strNotificationDownloading,
                        (if (totalLength == 0L) 0 else savedLength * 100 / totalLength).toInt()), Toast.LENGTH_SHORT).show()
            }

            override fun onDownloadSuccess(patchFilePath: String) {
                Toast.makeText(applicationContext, patchFilePath, Toast.LENGTH_SHORT).show()
                //                Beta.applyDownloadedPatch();
            }

            override fun onDownloadFailure(msg: String) {
                Toast.makeText(applicationContext, msg, Toast.LENGTH_SHORT).show()
            }

            override fun onApplySuccess(msg: String) {
                Toast.makeText(applicationContext, msg, Toast.LENGTH_SHORT).show()
            }

            override fun onApplyFailure(msg: String) {
                Toast.makeText(applicationContext, msg, Toast.LENGTH_SHORT).show()
            }

            override fun onPatchRollback() {
                Toast.makeText(applicationContext, "onPatchRollback", Toast.LENGTH_SHORT).show()
            }
        }

        // 这里实现SDK初始化，appId替换成你的在Bugly平台申请的appId
        // 调试时，将第三个参数改为true
        Bugly.init(this, "720f47af4a", true);
        Bugly.setIsDevelopmentDevice(getApplicationContext(), true);
        /**
         * 以下是 AndroidAutoSize 可以自定义的参数, {@link AutoSizeConfig} 的每个方法的注释都写的很详细
         * 使用前请一定记得跳进源码，查看方法的注释, 下面的注释只是简单描述!!!
         */
//        AutoSizeConfig.getInstance()

                //是否打印 AutoSize 的内部日志, 默认为 true, 如果您不想 AutoSize 打印日志, 则请设置为 false
//                .setLog(false)
                //是否使用设备的实际尺寸做适配, 默认为 false, 如果设置为 false, 在以屏幕高度为基准进行适配时
                //AutoSize 会将屏幕总高度减去状态栏高度来做适配, 如果设备上有导航栏还会减去导航栏的高度
                //设置为 true 则使用设备的实际屏幕高度, 不会减去状态栏以及导航栏高度
//                .setUseDeviceSize(true)

        Hawk.init(this).build()
        TimberManager.initialize()
    }


    override fun applicationInjector(): AndroidInjector<out DaggerApplication> {
        return DaggerAppComponent.builder().application(this).build()
    }
}